package giotto2D.filters.merge;

import giotto2D.filters.AbstractFilter;
import java.awt.image.*;
import java.awt.*;
import giotto2D.core.*;


public class ColorKey extends AbstractFilter
{
	private static float range;
	private static float slope;

	public static void setParams( int range_, int slope_ )
	{
		range = (float) range_;
		slope = (float) slope_;
	}

	//--- Will fail if source has some dimension bigger then destinion.
	public static void filter( BufferedImage destination, Color key )
	{
		int rgb;
		int a;
		int r;
		int g;
		int b;

		int cr = key.getRed();
		int cg = key.getRed();
		int cb = key.getBlue();

		float d;
		int[] dPix = getBank( destination );

		for( int i = 0; i < dPix.length; i++ )
		{
			r = ( dPix[ i ] >> red ) & 0xff;
			g = ( dPix[ i ] >> green ) & 0xff;
			b = dPix[ i ]  & 0xff;

			d = GeometricFunctions.distance3D( r, g, b, cr, cg, cb);
			if( d < range ) a = 0;
			else if( d > range + slope ) a = 255;
			else a = (int) ((( d - range ) / slope ) * 255.0f);
		
			dPix[ i ] = ( a << alpha ) | ( dPix[ i ] & alpha_mask );
		}
	}

}//end class